#!/usr/bin/python
#-*-coding:utf-8-*-

import cookielib, threading, random, paramiko, time
import lib
import lib.logConfig as log
from lib.protocol import *
from lib.threadCenter import *
from lib.interfaceFunc import *

def CreateGroup(cpu, companyAccount, companyCode, companyPassword, startGroupNum, endGroupNum, userNum):
    serverIP = cpu["data"]._const["HTTP"]["HOST"]
    # 登录后cookie的信息存储，用于携带cookie信息到服务端，然后服务端会在cookie中存入JSESSIONID并返回
    cookies     = cookielib.CookieJar()
    cookies.set_cookie(make_cookie("fnRemeberAdminCode", "true", serverIP+'/company/'))
    cookies.set_cookie(make_cookie("fnAdminCode", companyAccount, serverIP+'/company/'))
    cookies.set_cookie(make_cookie("fnAdminPassword",  companyPassword, serverIP+'/company/'))
    opener      = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies))
    urllib2.install_opener(opener)

    # 发起登录请求
    response    = FormHttpPostFirst_returnData('http://'+serverIP+'/company/login.do', {'adminCode': companyAccount, 'password': companyPassword})
    if response["status"] == 100:
        log.info("Login success...")

        response = FormHttpPostFirst_returnData('http://' + serverIP + '/company/companyMemberAddPage_organizeTree.do',{})
        departmentId = response["data"][0]["id"]
        departmentName = response["data"][0]["name"].encode("utf-8")

        for i in range(startGroupNum, endGroupNum+1):
            groupName = "TG"+str(i)
            jsonAck = FormHttpPostFirst_returnData('http://' + serverIP + '/company/companyGroupAddPage_companyGroupAdd.do', {"groupName": groupName,"status":1})
            print jsonAck
            newGroupId = jsonAck["data"]

            for j in range(userNum*(i-1)+1,userNum*i+1):
                num = 10000 + j
                imei = int(imeiAssemble(companyCode)) + num*10000000000
                userCode = companyCode + str(num)
                userName = ""
                if (len(userCode) <= 3):
                    userName = userCode + str(num)
                else:
                    userName = userCode[0:3] + str(num)

                data = {"userCode": userCode, 'userName': userName, 'userType': '2', 'sex': '0', 'imei': str(imei),
                        'mobile': '18758038709', 'status': '1', 'groupIds': newGroupId, 'defaultGroupId': newGroupId,
                        'level': '100', 'permissions': '{5},{4},{3},{2},{1}', "departmentId": departmentId,
                        "departmentName": departmentName}
                response = FormHttpPost('http://' + serverIP + '/company/companyMemberAddPage_companyMemberAdd.do', data)
                if response == 100:
                    log.info(u"账号创建成功!")
                else:
                    log.info(u"账号创建失败!")


if __name__ == "__main__":
    cpu = {}
    cpu["data"] = lib.DataCenter()
    #参数说明：
    #参数1：全局变量
    #参数2：企业名称
    #参数3：企业标识
    #参数4：企业密码
    #参数5：开始群组
    #参数6：结束群组
    #参数7：每组中的人数
    CreateGroup(cpu, "fanxs1", "fanxs1", "123456", 1, 10, 500)







    
